package com.example.taobaounion.base

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.example.taobaounion.R

/**
 *@author : Administrator
 *@descreption : fragment基类
 */
abstract class BaseFragment : Fragment() {
    enum class State {
        LOADING, SUCCESS, ERROR, NONE, BLANK
    }

    private lateinit var noDataView: View
    private lateinit var mOnClickListener: View.OnClickListener
    private lateinit var blankView: View
    private lateinit var errorView: View
    private lateinit var successView: View
    private lateinit var loadingView: View
    private var currentState = State.NONE
    private var mResourceId = 0
    private var mRootViewID = R.layout.fragment_base_frame
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.i("TAG", "huge: onCreateView ");
        //setRootView(R.layout.fragment_base_frame)
        val rootView = inflater.inflate(mRootViewID, container, false)
        val frameLayout = rootView.findViewById<FrameLayout>(R.id.state_container)
        //添加所有的View到rootView
        addAllViewToRoot(inflater, frameLayout)
        //设置最初的State为NONE
        setViewState(State.BLANK)
        setPresenter()
        //初始化继承者的View
        initView(rootView)
        //初始化继承者的数据
        initData()
        return rootView
    }

    open fun setPresenter() {

    }

    //让子类可以自定义rootView的布局
    open fun setRootView(rootViewID: Int) {
        mRootViewID = rootViewID
    }

    //添加所有的View到rootView
    private fun addAllViewToRoot(inflater: LayoutInflater, frameLayout: FrameLayout) {
        //Loading页面
        loadingView = addLoadingView(inflater, frameLayout)
        frameLayout.addView(loadingView)
        //Success页面
        successView = addSuccessView(inflater, frameLayout)
        frameLayout.addView(successView)
        //Error页面
        errorView = addErrorView(inflater, frameLayout)
        frameLayout.addView(errorView)
        //Blank页面 空白页面
        blankView = addBlankView(inflater, frameLayout)
        frameLayout.addView(blankView)
        //None 没有数据
        noDataView = addNoDataView(inflater, frameLayout)
        frameLayout.addView(noDataView)
        //设置初始全部的Fragment默认为空白页面
    }


    //重新加载数据 继承者可以重新该方法
    open fun reLoadData() {

    }

    private fun addLoadingView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.loading_view, container, false)
    }

    private fun addErrorView(inflater: LayoutInflater, container: ViewGroup?): View {
        val errorRoot = inflater.inflate(R.layout.error_view, container, false)
        //为网络错误的页面设置点击事件.具体实现可以在子类中重写reLoadData方法实现
        errorRoot.findViewById<LinearLayout>(R.id.reload).setOnClickListener {
            reLoadData()
        }
        return errorRoot
    }

    private fun addBlankView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.blank_view, container, false)
    }

    private fun addNoDataView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.no_data_view, container, false)
    }

    private fun addSuccessView(inflater: LayoutInflater, container: ViewGroup?): View {
        setRootView()
        return inflater.inflate(mResourceId, container, false)
    }

    fun setViewState(state: State) {
        Log.i("TAG", "State===>>>${state} ");
        //空白
        if (state == State.BLANK) {

            blankView.visibility = View.VISIBLE
        } else {
            blankView.visibility = View.GONE
        }
        //没有数据
        if (state == State.NONE) {
            noDataView.visibility = View.VISIBLE
        } else {
            noDataView.visibility = View.GONE
        }
        //错误
        if (state == State.ERROR) {
            errorView.visibility = View.VISIBLE
        } else {
            errorView.visibility = View.GONE
        }
        //加载
        if (state == State.LOADING) {
            loadingView.visibility = View.VISIBLE
        } else {
            loadingView.visibility = View.GONE
        }
        //成功
        if (state == State.SUCCESS) {
            successView.visibility = View.VISIBLE
        } else {
            successView.visibility = View.GONE
        }

    }

    abstract fun initView(container: View)

    abstract fun initData()

    private fun setRootView() {
        mResourceId = setLayoutResourcesID()
    }


    abstract fun setLayoutResourcesID(): Int



}